﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace GabageFactory.Models
{
    public class FactoryWeatherReposity : Reposity
    {
        public Factory_daily_weather_info GetDailyWeatherInfo(int factoryId, DateTime date)
        {
            var dailyWeather = from s in _dataContext.Factory_daily_weather_infos
                             where factoryId == s.factory_id && s.date == date.Date
                             select s;

            if (dailyWeather != null && dailyWeather.Count() > 0)
            {
                return dailyWeather.First();
            }
            else
            {
                return null;
            }
        }

        public Factory_daily_weather_info CreateDailyWeather(Factory_daily_weather_info dailyWeather)
        {
            _dataContext.Factory_daily_weather_infos.InsertOnSubmit(dailyWeather);
            SubmitChanges();
            return dailyWeather;
        }

        public bool DeleteDailyWeather(Factory_daily_weather_info dailyWeather)
        {
            _dataContext.Factory_daily_weather_infos.DeleteOnSubmit(dailyWeather);
            return true;
        }

        public IDictionary<DateTime, Factory_daily_weather_info> QueryWeathers(long factoryId, DateTime fromDate, DateTime toDate)
        {
            var dailyWeather = from s in _dataContext.Factory_daily_weather_infos
                               where factoryId == s.factory_id && s.date >= fromDate && s.date < toDate  
                               select s;

            IDictionary<DateTime, Factory_daily_weather_info> infos = new Dictionary<DateTime, Factory_daily_weather_info>();
            foreach (Factory_daily_weather_info info in dailyWeather)
            {
                infos.Add(info.date, info);
            }

            return infos;
        }
    }
}